<template>
    <Card :bordered="false" dis-hover class="ivu-mt">
        <Row>
            <Col :xl="{span: 12, offset: 6}" :lg="{span: 12, offset: 6}" :md="{span: 16, offset: 4}"
                 :sm="{span: 20, offset: 2}" :xs="24">
                <Form ref="form" :model="data" :rules="rules" class="ivu-mt" :label-width="labelWidth"
                      :label-position="labelPosition">
                    <FormItem label="用印文件名：" prop="adtrtName" label-for="adtrtName">
                        <Input v-model="data.adtrtName" placeholder="用印文件名（必填）" element-id="adtrtName"/>
                    </FormItem>

                    <FormItem label="文件份数：" prop="adtrtCount" label-for="adtrtCount">
                        <Input v-model="data.adtrtCount" placeholder="文件份数（必填）" element-id="adtrtCount"/>
                    </FormItem>

                    <FormItem label="加盖印章类型：" prop="ZhangType" label-for="ZhangType">
                        <Select v-model="data.ZhangType" placeholder="加盖印章类型" element-id="ZhangType">
                            <Option value="1">公章</Option>
                            <Option value="2">合同章</Option>
                            <Option value="3">其他</Option>
                        </Select> </FormItem>
                    <FormItem label="用印原因：" prop="adtrtReson" label-for="adtrtReson">
                        <Input  type="textarea" :rows="4"  v-model="data.adtrtReson" placeholder="请输入申请领用原因描述" element-id="adtrtReson"/>
                    </FormItem>
                    <FormItem>
                        <Button class="ivu-ml" @click="handleReset(data)">取消</Button>
                        <Button class="ivu-ml" type="primary" :loading="loading" @click="handleSubmit">确定</Button>
                    </FormItem>

                </Form>
            </Col>
        </Row>
    </Card>

</template>

<script>
    import Administrative from "@/api/Administrative";
	export default {
		data() {
			return {
				trafficType: '',
				formItem: {
					checkbox: [],
				},
				data: {
                    adtrtName: '',
                    adtrtCount: '',
                    ZhangType: '',
                    adtrtReson: '',
				},
				loading: false,
				rules: {
                    adtrtName: [
						{required: true, message: '请输入名称（必填', trigger: 'blur'}
					],
                    adtrtCount: [
						{required: true, message: '请输入数量（必填', trigger: 'blur'}
					],
                    adtrtReson: [
						{required: true, message: '请输入申请原因描述', trigger: 'blur'}
					], ZhangType: [
						{required: true, message: '加盖印章类型', trigger: 'change'}
					],
				},
			}
		},
		computed: {
			labelWidth() {
				return this.isMobile ? undefined : 140;
			},
			labelPosition() {
				return this.isMobile ? 'top' : 'left';
			}
		},
		created() {
			let baseData = this.$route.params.item;
			let myIndex = this.$route.params.myIndex;
			console.log(myIndex);
			//是否编辑渲染
			if (myIndex) {
				this.data.contractId = baseData.positionName;
				this.data.mechanismOwner = baseData.mechanismOwner;

			}
		},
		methods: {
			//新增
			handleSubmit() {
				this.$refs.form.validate((valid) => {
					if (valid) {
						this.loading = true;
                        Administrative.insertCarOrZang({
                            adtrtType: 3, //行政类型（1：物品领用，2：用车申请，3：永章申请）
                            // status: 1 //审核状态（1：通过，2：未审核
                            adtrtName:this.data.adtrtName,
                            adtrtCount: this.data.adtrtCount,
                            ZhangType: this.data.ZhangType,
                            adtrtReson: this.data.adtrtReson,
                            userId:this.$store.state.admin.user.info.userId,

                        })
                            .then(res => {
                                // 返回数据
                                this.loading = false;
                                this.$Message.success('新增成功');
                                this.$router.push('/administrative/chapter')
                            })
                            .catch(err => {
                                // 异常情况
                            })
					} else {
						this.loading = false;
					}
				});
			},
			// 新增数据
			handleCreate () {
				this.$refs.create.validate((valid) => {
					if (valid) {
						if (this.updateIndex < 0) {
							// 新建
							const mockData = this.mockOneData();
							mockData.desc = this.createData.desc;
							mockData.date = new Date();
							this.list.splice(0, 0, mockData);
							this.$Message.success('创建成功');
						} else {
							// 修改
							const updateItemName = this.dataWithPage[this.updateIndex].name;
							const updateItemIndex = this.list.findIndex(item => item.name === updateItemName);
							this.list[updateItemIndex].desc = this.createData.desc;
							this.$Message.success('修改成功');
						}

						this.showCreate = false;
						this.creating = false;
						this.$nextTick(() => {
							this.creating = true;
						});
					} else {
						this.creating = false;
						this.$nextTick(() => {
							this.creating = true;
						});
					}
				});
			},
			//取消
			handleReset(name) {
				// this.$refs[name].resetFields();
				this.$router.push('/administrative/chapter')
			},

		}

	}
</script>

<style scoped>

</style>
